using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ColorPicker
{
    public partial class AdvancedColorPickerDialog : UserControl, IComponentConnector
    {
        private byte[] _pixels;

        public Color CurrentColor = Colors.Gray;

        public static DependencyProperty SelectedCurrentColorProperty = DependencyProperty.Register("SelectedCurrentColor", typeof(SolidColorBrush), typeof(ColorPickerControlView), new PropertyMetadata(Brushes.White));

        private readonly TranslateTransform _markerTransform = new TranslateTransform();

        public SolidColorBrush SelectedCurrentColor
        {
            get
            {
                return (SolidColorBrush)GetValue(SelectedCurrentColorProperty);
            }
            set
            {
                SetValue(SelectedCurrentColorProperty, value);
            }
        }

        public bool AllowsTransparency
        {
            set
            {
                if (!value)
                {
                    Container.Margin = new Thickness(0.0);
                    Container.CornerRadius = new CornerRadius(0.0);
                }
            }
        }

        public event EventHandler DialogResultEvent;

        public event DragDeltaEventHandler Drag;

        private void OnDragDelta(DragDeltaEventArgs e)
        {
            DragDeltaEventHandler drag = this.Drag;
            if (drag != null)
            {
                drag(this, e);
            }
        }

        public AdvancedColorPickerDialog()
        {
            base.DataContext = this;
            InitializeComponent();
            brightnessSlider.ValueChanged += BrightnessSliderValueChanged;
            colorMarker.RenderTransform = _markerTransform;
            colorMarker.RenderTransformOrigin = new Point(1.0, 1.0);
            borderColorChart.Cursor = Cursors.Cross;
            brightnessSlider.Value = 0.5;
        }

        private void moveThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            OnDragDelta(e);
        }

        private void OnDialogResultEvent(DialogEventArgs e)
        {
            EventHandler dialogResultEvent = this.DialogResultEvent;
            if (dialogResultEvent != null)
            {
                dialogResultEvent(this, e);
            }
        }

        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                CroppedBitmap croppedBitmap = new CroppedBitmap((BitmapSource)((Image)e.Source).Source, new Int32Rect((int)Mouse.GetPosition(e.Source as Image).X, (int)Mouse.GetPosition(e.Source as Image).Y, 1, 1));
                _pixels = new byte[4];
                try
                {
                    croppedBitmap.CopyPixels(_pixels, 4, 0);
                    UpdateCurrentColor();
                    UpdateMarkerPosition();
                }
                catch
                {
                }
                UpdateSlider();
            }
            catch (Exception)
            {
            }
        }

        private void UpdateCurrentColor()
        {
            CurrentColor = Color.FromRgb(_pixels[2], _pixels[1], _pixels[0]);
            currentColorBorder.Background = new SolidColorBrush(Color.FromRgb(_pixels[2], _pixels[1], _pixels[0]));
            brightnessSlider.Value = 0.5;
            SelectedCurrentColor = new SolidColorBrush(CurrentColor);
        }

        private void UpdateMarkerPosition()
        {
            _markerTransform.X = Mouse.GetPosition(borderColorChart).X - borderColorChart.ActualWidth / 2.0;
            _markerTransform.Y = Mouse.GetPosition(borderColorChart).Y - borderColorChart.ActualHeight / 2.0;
        }

        private void BrightnessSliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (_pixels == null)
            {
                _pixels = new byte[3];
                _pixels[2] = CurrentColor.R;
                _pixels[1] = CurrentColor.G;
                _pixels[0] = CurrentColor.B;
            }
            Color color = Color.FromRgb(_pixels[2], _pixels[1], _pixels[0]);
            float num = (float)e.NewValue;
            float r;
            float g;
            float b;
            if (num >= 0.5f)
            {
                r = color.ScR + (1f - color.ScR) * (num - 0.5f) * 2f;
                g = color.ScG + (1f - color.ScG) * (num - 0.5f) * 2f;
                b = color.ScB + (1f - color.ScB) * (num - 0.5f) * 2f;
            }
            else
            {
                r = color.ScR * num * 2f;
                g = color.ScG * num * 2f;
                b = color.ScB * num * 2f;
            }
            CurrentColor = Color.FromScRgb(1f, r, g, b);
            currentColorBorder.Background = new SolidColorBrush(CurrentColor);
        }

        private void UpdateSlider()
        {
            Color color = Color.FromRgb(_pixels[2], _pixels[1], _pixels[0]);
            float r = 1f * color.ScR;
            float g = 1f * color.ScG;
            float b = 1f * color.ScB;
            Border border = brightnessSliderBorder;
            LinearGradientBrush linearGradientBrush = border.Background as LinearGradientBrush;
            linearGradientBrush.StartPoint = new Point(0.0, 1.0);
            linearGradientBrush.EndPoint = new Point(0.0, 0.0);
            linearGradientBrush.GradientStops[0].Color = Color.FromScRgb(1f, 0f, 0f, 0f);
            linearGradientBrush.GradientStops[1].Color = Color.FromScRgb(1f, r, g, b);
            linearGradientBrush.GradientStops[2].Color = Color.FromScRgb(1f, 1f, 1f, 1f);
        }

        private void Image_MouseMove(object sender, MouseEventArgs e)
        {
            if (Mouse.LeftButton == MouseButtonState.Pressed && e.Source.GetType().Equals(typeof(Image)))
            {
                int num = (int)Mouse.GetPosition(e.Source as Image).X;
                double width = ((Image)e.Source).Source.Width;
                double height = ((Image)e.Source).Source.Height;
                if (num < 0)
                {
                    num = 0;
                }
                else if ((double)num >= width)
                {
                    num = (int)(width - 1.0);
                }
                int num2 = (int)Mouse.GetPosition(e.Source as Image).Y;
                if (num2 < 0)
                {
                    num2 = 0;
                }
                else if ((double)num2 >= height)
                {
                    num2 = (int)(height - 1.0);
                }
                CroppedBitmap croppedBitmap = new CroppedBitmap((BitmapSource)((Image)e.Source).Source, new Int32Rect(num, num2, 1, 1));
                _pixels = new byte[4];
                try
                {
                    croppedBitmap.CopyPixels(_pixels, 4, 0);
                    UpdateMarkerPosition();
                    UpdateCurrentColor();
                    Mouse.Synchronize();
                }
                catch
                {
                }
                UpdateSlider();
            }
        }

        private void Ok_Click(object sender, RoutedEventArgs e)
        {
            OnDialogResultEvent(new DialogEventArgs
            {
                SelectedColor = SelectedCurrentColor,
                DialogResult = DialogResult.Ok
            });
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            OnDialogResultEvent(new DialogEventArgs
            {
                DialogResult = DialogResult.Cancel
            });
        }
    }
}
